﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace Innoapps.FileTransfer.Helpers
{
    public class XmlSerializerHelper<T>
    {

        #region Variables

        public Type _type;

        #endregion

        #region Constructors

        public XmlSerializerHelper()
        {
            _type = typeof(T);
        }

        #endregion

        #region Methods
        /// <summary>
        /// Creates a new xml document object for the specified object
        /// </summary>
        /// <param name="xmlDoc">Return: Xml document</param>
        /// <param name="obj">Object to be converted to xml</param>
        /// <param name="schema">Base xml schema to be used</param>
        public void Save(XmlDocument xmlDoc, object obj, string schema)
        {

            XmlSerializer ser = new XmlSerializer(_type, schema);

            xmlDoc = null;

            using (MemoryStream memStm = new MemoryStream())
            {
                ser.Serialize(memStm, obj);

                memStm.Position = 0;

                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreWhitespace = true;

                using (var xtr = XmlReader.Create(memStm, settings))
                {
                    xmlDoc = new XmlDocument();
                    xmlDoc.Load(xtr);
                }
            }


        }
        /// <summary>
        /// Creates a new physical xml document for the specified object
        /// </summary>
        /// <param name="path">Path to save the physical xml document</param>
        /// <param name="obj">Object to be converted to xml</param>
        /// <param name="schema">Base xml schema to be used</param>
        public void Save(string path, object obj, string schema)
        {
            using (TextWriter textWriter = new StreamWriter(path, false))
            {
                XmlSerializer serializer = new XmlSerializer(_type, schema);
                serializer.Serialize(textWriter, obj);
            }

        }
        /// <summary>
        /// Reads an xml document object and returns the resulting object
        /// </summary>
        /// <param name="xmlDoc">Xml document to be read</param>
        /// <param name="schema">Xml schema to be used</param>
        /// <returns></returns>
        public T Read(XmlDocument xmlDoc, string schema)
        {
            T result;
            
            using (XmlReader reader = new XmlNodeReader(xmlDoc))
            {
                XmlSerializer deserializer = new XmlSerializer(_type, schema);
                result = (T)deserializer.Deserialize(reader);
            }
            return result;

        }
        /// <summary>
        /// Reads a physical xml document from disk and returns the resulting object
        /// </summary>
        /// <param name="path">Path to the xml document to be read</param>
        /// <param name="schema">Xml schema to be used</param>
        /// <returns></returns>
        public T Read(string path, string schema)
        {
            T result;
            using (TextReader textReader = new StreamReader(path))
            {
                XmlSerializer deserializer = new XmlSerializer(_type, schema);
                result = (T)deserializer.Deserialize(textReader);
            }
            return result;

        }

        #endregion

    }
}
